package com.zzjson.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.io</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月25日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class FileRead {
	public static void main(String[] args) throws IOException {
		FileInputStream fileInputStream = new FileInputStream(new File("/Users/zzy/git/learning/learning-io/src/main/java/com/zzjson/io/1.txt"));
		ByteBuffer byteBuffer = ByteBuffer.allocate(10);

		output("初始化", byteBuffer);

		FileChannel channel = fileInputStream.getChannel();
		int read = channel.read(byteBuffer);

		output("调用read", byteBuffer);

		byteBuffer.flip();
		output("调用flip", byteBuffer);

		while (byteBuffer.remaining() > 0) {
			byteBuffer.get();
			output("调用get", byteBuffer);
		}

		byteBuffer.clear();
		output("调用clear", byteBuffer);
	}

	public static void output(String step, Buffer buffer) {
		System.out.println(step + " : ");
		System.out.print("capacity: " + buffer.capacity() + ", ");
		System.out.print("position: " + buffer.position() + ", ");
		System.out.println("limit: " + buffer.limit());
		System.out.println();
	}
}
